<?php

class Controller_Company extends Controller_Website {

    public function action_list() {
        $m_user = Model::factory('user');
        
        $where = array();
        $where['ORDER'] = 'create_time DESC';
        $where['type'] = 2;
        $total = $m_user->count($where);
        $pager = new Pager($total, 20);
        $list = $m_user->select($pager->offset, $pager->size, $where)->as_array();

        $this->content = View::factory('user/company');
        $this->content->list = $list;
        $this->content->pager = $pager;
        
    }
    
    public function action_del() {
        $id = $_GET['id'];
        $model = Model::factory('user');
        $ret = $model->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_edit() {
        $id = $_GET['id'];
        $m_user = Model::factory('user');
        $userInfo = $m_user->getRowById($id);
    
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $ret = $m_user->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('user/list');
            }
        }
        $this->content = View::factory('user/edit');
        $this->content->userInfo = $userInfo;
    }
    
    protected function _get_data($post) {
        $data = array();
    
        $data['username'] = Arr::get($post, 'username');
    
        $password = Arr::get($post, 'password');
        if (!empty($password)) {
            $auth = new Model_Auth();
            $password = $auth->hash($password);
            $data['password'] = $password;
        }
    
        $data = array_filter($data, 'strlen');
        return $data;
    }
}

